﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using QRCoder;
using SkiaSharp;

namespace Best.SmartCms.Utility
{
    // <summary>
    // 二维码生成服务
    // </summary>
    public class QrCoderHelper
    {
        /// <summary>
        /// 生成byte二维码
        /// </summary>
        /// <param name="msg"></param>
        public static byte[] CreateByteQrCode(string msg)
        {
            // 生成二维码的内容
            string strCode = msg;
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
            QRCode qrcode = new QRCode(qrCodeData);

            // qrcode.GetGraphic 方法可参考最下发“补充说明”
            Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
            MemoryStream ms = new MemoryStream();
            qrCodeImage.Save(ms, ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            return arr;
        }


        /// <summary>
        /// 生成base64二维码
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string CreateBase64QrCode(string msg)
        {
            //// 生成二维码的内容
            //string strCode = msg;
            //QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
            //QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
            //QRCode qrcode = new QRCode(qrCodeData);

            //// qrcode.GetGraphic 方法可参考最下发“补充说明”
            //Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
            //MemoryStream ms = new MemoryStream();
            //qrCodeImage.Save(ms, ImageFormat.Jpeg);
            //byte[] arr = new byte[ms.Length];
            //ms.Position = 0;
            //ms.Read(arr, 0, (int)ms.Length);
            //ms.Close();
            //var strbaser64 = Convert.ToBase64String(arr);
            //return "data:image/png;base64," + strbaser64;


            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(msg, QRCodeGenerator.ECCLevel.Q);
            BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData);
            byte[] arr = qrCode.GetGraphic(5);
            MemoryStream ms = new MemoryStream();
            var qrCodeImage = SKBitmap.Decode(arr);
            var byt = SKImage.FromBitmap(qrCodeImage);
            byt.Encode(SKEncodedImageFormat.Png, 80).SaveTo(ms);
            byte[] arr2 = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr2, 0, (int)ms.Length);
            ms.Close();
            var strbaser64 = Convert.ToBase64String(arr2);
            return "data:image/png;base64," + strbaser64;
        }



        public static string CreateBitmapByteArr()
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
            BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData);
            byte[] arr = qrCode.GetGraphic(20);
            //From here on, you can implement your platform-dependent byte[]-to-image code 

            //e.g. Windows 10 - Full .NET Framework
            MemoryStream ms = new MemoryStream();
            //qrCodeImage.Save(ms, ImageFormat.Jpeg);
            //byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            var strbaser64 = Convert.ToBase64String(arr);
            return "data:image/png;base64," + strbaser64;




        }


    }
}
